<template>
    <div>
        <div>
            <el-input
                size="small"
                placeholder="添加职称..."
                prefix-icon="el-icon-plus"
                v-model="jl.name"
                class="addJlInput"
                @keydown.enter.native="addJobLevel">
            </el-input>
            <el-select v-model="jl.titleLevel" placeholder="职称级别"
                       size="small" style="margin-left: 6px;margin-right: 6px"
            >
                <el-option
                    v-for="item in titleLevels"
                    :key="item"
                    :label="item"
                    :value="item">
                </el-option>
            </el-select>
            <el-button type="primary" icon="el-icon-plus" size="small" @click="addJobLevel">添加</el-button>
        </div>
        <div class="JobLevelManaMain">
            <el-table
                :data="jobLevels"
                border
                stripe
                size="small"
                @selection-change="handleSelectionChange"
                style="width: 70%">
                <el-table-column
                    type="selection"
                    width="55">
                </el-table-column>
                <el-table-column
                    prop="id"
                    label="编号"
                    width="55">
                </el-table-column>
                <el-table-column
                    prop="name"
                    label="职称名称"
                    width="120">
                </el-table-column>
                <el-table-column
                    prop="titleLevel"
                    label="职称级别"
                    width="150">
                </el-table-column>
                <el-table-column
                    prop="createDate"
                    label="创建时间"
                    width="200">
                </el-table-column>
                <el-table-column
                    prop="enabled"
                    label="是否启用"
                    width="150">
                    <template slot-scope="scope">
                        <el-tag v-if="scope.row.enabled" type="success">已启用
                        </el-tag>
                        <el-tag v-else type="danger">未启用</el-tag>
                    </template>
                </el-table-column>
                <el-table-column
                    label="操作">
                    <template slot-scope="scope">
                        <el-button
                            size="mini"
                            @click="showEditView(scope.$index, scope.row)">编辑
                        </el-button>
                        <el-button
                            size="mini"
                            type="danger"
                            @click="handleDelete(scope.row)">
                            删除
                        </el-button>
                    </template>
                </el-table-column>
            </el-table>
        </div>
        <el-button type="danger" style="margin-top: 8px" size="small"
                   :disabled="this.multipleSelection.length==0" @click="deleteMany">批量删除
        </el-button>
        <el-dialog
            title="编辑职称"
            :visible.sync="dialogVisible"
            width="30%">
            <div>
                <table>
                    <tr>
                        <td>
                            <el-tag>职称名</el-tag>
                        </td>
                        <td>
                            <el-input v-model="updateJl.name" size="small"></el-input>
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <el-tag>职称级别</el-tag>
                        </td>
                        <td>
                            <el-select v-model="updateJl.titleLevel"
                                       placeholder="职称等级" size="small"
                                       style="margin-left: 6px;margin-right: 6px">
                                <el-option
                                    v-for="item in titleLevels"
                                    :key="item"
                                    :label="item"
                                    :value="item">
                                </el-option>
                            </el-select>
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <el-tag>是否启用</el-tag>
                        </td>
                        <td>
                            <el-switch v-model="updateJl.enabled" active-color="#13ce66" inactive-color="#ff4949"
                                       active-text="启用" inactive-text="禁用" style="margin-left: 8px">
                            </el-switch>
                        </td>
                    </tr>
                </table>
            </div>
            <span slot="footer" class="dialog-footer">
                <el-button size="small" @click="dialogVisible = false">取 消</el-button>
                <el-button size="small" type="primary" @click="doUpdate">确 定</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
export default {
    name: "JoblevelMana",
    data() {
        return {
            dialogVisible: false,
            multipleSelection: [],
            jl: {
                name: '',
                titleLevel: ''
            },
            titleLevels: [
                '正高级',
                '副高级',
                '中级',
                '初级',
                '员级',
            ],
            jobLevels: [],
            updateJl: {
                name: '',
                titleLevel: '',
                enabled: false
            }
        }
    },
    mounted() {
        this.initJobLevels();
    },
    methods: {
        initJobLevels() {
            this.getRequest('/system/basic/joblevel/').then(resp => {
                if (resp) {
                    this.jobLevels = resp;
                }
            })
        },
        addJobLevel() {
            if (this.jl.name && this.jl.titleLevel) {
                this.postRequest('/system/basic/joblevel/', this.jl).then(resp => {
                    if (resp) {
                        this.initJobLevels();
                        this.jl.name = '';
                        this.jl.titleLevel = '';
                    }
                })
            } else {
                this.$message.error('请检查职称名称和是否级别合法！');
            }
        },
        showEditView(index, data) {
            //数据拷贝得到updateJl
            Object.assign(this.updateJl, data);
            this.updateJl.createDate = '';
            this.dialogVisible = true;
        },
        handleDelete(data) {
            this.$confirm('此操作将永久删除该【' + data.name + '】职称, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                this.deleteRequest('/system/basic/joblevel/' + data.id).then(resp => {
                    if (resp) {
                        this.initJobLevels();
                    }
                })
            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消删除'
                });
            });
        },
        handleSelectionChange(val) {
            this.multipleSelection = val;
        },
        doUpdate() {
            this.putRequest('/system/basic/joblevel/', this.updateJl).then(resp => {
                if (resp) {
                    this.initJobLevels();
                    this.updateJl.name = '';
                    this.updateJl.titleLevel = '';
                    this.dialogVisible = false;
                }
            })
        },
        deleteMany() {
            this.$confirm('此操作将永久删除该【' + this.multipleSelection.length + '】条职称, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                //?ids=1&ids=2&....
                let ids = '?';
                this.multipleSelection.forEach(item => {
                    ids += 'ids=' + item.id + '&';
                });
                this.deleteRequest('/system/basic/joblevel/' + ids).then(resp => {
                    if (resp) {
                        this.initJobLevels();
                    }
                })
            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消删除'
                });
            });
        },
    }
};
</script>

<style scoped>
.addJlInput {
    width: 300px;
    margin-right: 8px;
}

.JobLevelManaMain {
    margin-top: 10px;
}

</style>
